/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.adapters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import android.content.Context;
import dk.redia.lib.log.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;
import dk.redia.libraryapp.R;
import dk.redia.libraryapp.model.Reservation;
import dk.redia.libraryapp.model.ReservationBranch;
import dk.redia.libraryapp.model.ReservationBranchModel;
import dk.redia.libraryapp.model.ReservationBranchModelListener;
import dk.redia.libraryapp.net.RemoteListManager;
import dk.redia.libraryapp.net.ReservationsRemoteListManager;

public class MyReservationsAdapter extends RemoteListAdapter {

	private static final String logTag = "ReservationsAdapter";
	private Context context;
	//private List<Reservation> reservations;
	//private DataSetObserverCollection observers;
	//private boolean selecting;
	private Set<Integer> selected;
	//private List<Library> libraries;
	private List<ReservationBranch> reservationBranches;
	
	public MyReservationsAdapter(Context context) {
		super(new ReservationsRemoteListManager());
		//this.reservations = new ArrayList<Reservation>();
		this.context = context;
		//this.libraries = new ArrayList<Library>();
		this.selected = new HashSet<Integer>();
	    reservationBranches = new ArrayList<ReservationBranch>();
	    ReservationBranchModel.getInstance().getReservationBranches(new ReservationBranchModelListener() {
			
			public void getReservationsFailure(String message) {
				Log.e(logTag, "Failed to retrieve reservation branches: " + message);
			}
			
			public void getReservationBranchesResult(List<ReservationBranch> rbs) {
				reservationBranches = rbs;
			}
		});
		//LibraryModel.getInstance().getLibraries(new LibraryModelListener() {
		//	public void getLibrariesFailure(String message) {
		//		libraries = new ArrayList<Library>();
		//	}
		//	
		//	public void getLibrariesResult(List<Library> libraries) {
		//		MyReservationsAdapter.this.libraries = libraries;
		//	}
		//});
	}
	
	@Override
	public void reset() {
		//this.selecting = false;
		this.selected.clear();
		super.reset();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		if (convertView==null) {
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.myreservationsitem, null);
		} else {
			v = convertView;
		}
		ensureIndexLoad(position);
		int loadState = getLoadState(position);
		updateViewCache(position, null, v);
		v = getViewByState(v, loadState);
		v.setClickable(true);
		Reservation reservation = (Reservation)getItem(position);
		if (selected.contains(position)) {
			Log.d(MyReservationsAdapter.logTag, "Found selected reservation");
		}
		v.setTag(position);
		if (loadState==RemoteListManager.STATE_LOADED) {
			
			updateViewCache(position, reservation.getReservationId(), v);
			
//			ImageView selectedImageView = (ImageView)v.findViewById(R.id.myReservationsItemImageView);
			TextView titleTextView = (TextView)v.findViewById(R.id.myReservationsItemTitleTextView);
			TextView creatorTextView = (TextView)v.findViewById(R.id.myReservationsItemCreatorTextView);
			TextView queuePosTextView = (TextView)v.findViewById(R.id.myReservationsItemQueuePositionTextView);
			CheckBox selectedCheckbox = (CheckBox)v.findViewById(R.id.myReservationsItemSelectedCheckBox);
			TextView pickupExpireDateTextView = (TextView)v.findViewById(R.id.myReservationsItemPickupExpireDateTextView);
			TextView pickupExpireDateStaticTextView = (TextView)v.findViewById(R.id.myReservationsItemPickupExpireDateStaticTextView);
			TextView pickupBranchTextView = (TextView)v.findViewById(R.id.myReservationsItemPickupBranchTextView);
			TextView reservationNumberTextView = (TextView)v.findViewById(R.id.myReservationsReservationNumberTextView);
			TextView reservationNumberStaticTextView = (TextView)v.findViewById(R.id.myReservationsReservationNumberStaticTextView);
			TextView pickupBranchStaticTextView = (TextView)v.findViewById(R.id.myReservationsItemPickupBranchStaticTextView);
//			Button deleteButton = (Button)v.findViewById(R.id.myReservationsDeleteImageButton);
//			deleteButton.setTag(position);

			selectedCheckbox.setChecked(selected.contains(position));
			selectedCheckbox.setTag(position);
			
			String title = reservation.getTitle();
			String creator = reservation.getAuthor();
			//String queuePos = "";			
							
			titleTextView.setText(title);
			creatorTextView.setText(creator);
			
			ReservationBranch pickupBranch = ReservationBranch.findById(reservationBranches, reservation.getReservationPickupBranch());
			
			queuePosTextView.setVisibility(View.INVISIBLE);
			
			pickupExpireDateStaticTextView.setVisibility(View.INVISIBLE);
			pickupExpireDateTextView.setVisibility(View.INVISIBLE);
			pickupBranchTextView.setVisibility(View.INVISIBLE);
			pickupBranchStaticTextView.setVisibility(View.INVISIBLE);
			
			if (reservation.hasSelfServicePickupNo() && reservation.getSelfServicePickupNo().length()>0) {
				reservationNumberTextView.setText(reservation.getSelfServicePickupNo());
				reservationNumberStaticTextView.setVisibility(View.VISIBLE);
				reservationNumberTextView.setVisibility(View.VISIBLE);
			} else {
				reservationNumberStaticTextView.setVisibility(View.GONE);
				reservationNumberTextView.setVisibility(View.GONE);
			}
			
			// If there is a pickup expiration date
			// we assume that the queue number isn't relevant
			// as the material is waiting on a shelf somewhere
			if (reservation.hasPickupExpireDate()) {
				if (pickupBranch!=null) {
					pickupBranchStaticTextView.setVisibility(View.VISIBLE);
					pickupBranchTextView.setVisibility(View.VISIBLE);
					pickupBranchTextView.setText(pickupBranch.getName());
				}
				if (reservation.getPickupExpireDate()!=null) {
					String pickupExpireDateStr = new SimpleDateFormat("dd/MM yyyy").format(reservation.getPickupExpireDate());
					pickupExpireDateTextView.setText(pickupExpireDateStr);
				}
				pickupExpireDateStaticTextView.setVisibility(View.VISIBLE);
				pickupExpireDateTextView.setVisibility(View.VISIBLE);
				
			} else if (reservation.hasQueueNo()) {
				queuePosTextView.setVisibility(View.VISIBLE);
				queuePosTextView.setText(
					String.format(
						context.getResources().getString(R.string.myReservationsItemQueuPosText), 
						reservation.getQueueNo()
					)
				);				
			}
			


		} else {

		}
				
		return v;
	}

	public void setSelected(int position, boolean selected) {
		if (selected) {
			this.selected.add(position);
		} else {
			this.selected.remove(position);
		}
	}
		
	public boolean isSelected(int position) {
		return selected.contains(position);
	}
	

	@Override
	protected View getViewByState(View v, int state) {
//		ImageView selectedImageView = (ImageView)v.findViewById(R.id.myReservationsItemImageView);
		TextView titleTextView = (TextView)v.findViewById(R.id.myReservationsItemTitleTextView);
		TextView creatorTextView = (TextView)v.findViewById(R.id.myReservationsItemCreatorTextView);
		TextView queuePosTextView = (TextView)v.findViewById(R.id.myReservationsItemQueuePositionTextView);
//		Button deleteButton = (Button)v.findViewById(R.id.myReservationsDeleteImageButton);
		ProgressBar loadingProgressBar = (ProgressBar)v.findViewById(R.id.myReservationsItemLoadingProgressBar);
		TextView pickupExpireDateTextView = (TextView)v.findViewById(R.id.myReservationsItemPickupExpireDateTextView);
		TextView pickupBranchTextView = (TextView)v.findViewById(R.id.myReservationsItemPickupBranchTextView);
		TextView pickupExpireDateStaticTextView = (TextView)v.findViewById(R.id.myReservationsItemPickupExpireDateStaticTextView);
		CheckBox selectedCheckBox = (CheckBox)v.findViewById(R.id.myReservationsItemSelectedCheckBox);
		TextView reservationNumberStaticTextView = (TextView)v.findViewById(R.id.myReservationsReservationNumberStaticTextView);
		TextView reservationNumberTextView = (TextView)v.findViewById(R.id.myReservationsReservationNumberTextView);
		
//		deleteButton.setVisibility(View.GONE);
//		selectedImageView.setVisibility(View.GONE);
		
		if (state==RemoteListManager.STATE_LOADED) { 
			titleTextView.setVisibility(View.VISIBLE);
			creatorTextView.setVisibility(View.VISIBLE);
			queuePosTextView.setVisibility(View.VISIBLE);
			loadingProgressBar.setVisibility(View.GONE);
			selectedCheckBox.setVisibility(View.VISIBLE);
		} else {
			titleTextView.setVisibility(View.GONE);
			creatorTextView.setVisibility(View.GONE);
			queuePosTextView.setVisibility(View.GONE);
			loadingProgressBar.setVisibility(View.VISIBLE);
			pickupExpireDateTextView.setVisibility(View.GONE);
			pickupBranchTextView.setVisibility(View.GONE);
			pickupExpireDateStaticTextView.setVisibility(View.GONE);
			selectedCheckBox.setVisibility(View.GONE);
			reservationNumberStaticTextView.setVisibility(View.GONE);
			reservationNumberTextView.setVisibility(View.GONE);
		}

		return v;
	}

	public Set<Integer> getSelected() {
		return selected;
	}

	public int getSelectedCount() {
		return selected.size();
	}
}
